package org.six11.olive.collabclient.server;

import java.io.PrintWriter; // import java.util.List;
// import java.util.Map;
// import java.util.Set;
import java.util.Set;

import org.six11.util.io.Message;
import org.six11.util.adt.MultiState;

/**
 * 
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class PenHandler extends AppletMessageHandler {

  protected StateTool tool;

  /**
   * @param state
   */
  public PenHandler(MultiState state) {
    super(state);
    tool = new StateTool(state);
  }

  /**
   * Receives pen data, distributes it to everybody in the room except the sender, and adds the data
   * to the sketch buffer.
   */
  @Override
  public void handle(Message m, String sessionID, PrintWriter out) {
    m.addParam("personID", sessionID); // others use to disambiguate who is who
    String roomName = m.getParam("room");
    tool.addSessionToRoom(sessionID, roomName);
    Set<String> members = tool.getRoomMembership(roomName, false);
    if (members != null) {
      bug("Sending pen message to " + members.size() + " members of the room '" + roomName + "'");
      for (String friend : members) {
        if (!friend.equals(sessionID)) {
          tool.getMessageQueue(friend, true).add(m.copy());
        }
      }
    } else {
      bug("Egad... members is null.");
    }
  }

}
